package org.lyq.erp.config.readproperties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class ReadProperties {

	private static Logger logger = LoggerFactory.getLogger(ReadProperties.class);

	private Properties errorMessage;

	private Properties urlRoute;
	
	private Properties config;
	
	private Properties application;

	private static ReadProperties instance;

	private ReadProperties() {
		getErrorMessageProperties();
		getUrlRouteProperties();
	}

	public static synchronized ReadProperties getInstance() {

		if (instance == null)
			instance = new ReadProperties();

		return instance;
	}

	public Properties getErrorMessageProperties() {

		EncodedResource encodedResource = new EncodedResource(new ClassPathResource("errorMessage.properties"),
				"UTF-8");
		try {
			errorMessage = PropertiesLoaderUtils.loadProperties(encodedResource);
		} catch (IOException e) {
			logger.info("ErrorMessagePropertiese is null :" + e.getMessage());
		}
		return errorMessage;
	}

	public Properties getUrlRouteProperties() {

		EncodedResource encodedResource = new EncodedResource(new ClassPathResource("urlRoute.properties"),
				"UTF-8");
		try {

			urlRoute = PropertiesLoaderUtils.loadProperties(encodedResource);
		} catch (IOException e) {

			logger.info("UrlRouteProperties is null :" + e.getMessage());
		}
		return urlRoute;
	}
	
	public Properties getConfigProperties() {

		EncodedResource encodedResource = new EncodedResource(new ClassPathResource("config.properties"),
				"UTF-8");
		try {

			config = PropertiesLoaderUtils.loadProperties(encodedResource);
		} catch (IOException e) {

			logger.info("ConfigProperties is null :" + e.getMessage());
		}
		return config;
	}
	
	public synchronized Properties getApplicationProperties() {

		if (application == null) {
			InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");
			application = new Properties();
			try {
				application.load(inputStream);
			} catch (IOException e) {
				logger.info("application is null :" + e.getMessage());
			}
		}

		return application;
	}
}
